package dataAccessLayer.tests;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dataAccessLayer.dataBase.DBManager;
import dataAccessLayer.factoryMySQL.daos.DAOingredientsMySQL;
import dataAccessLayer.visualObject.VOingredient;
import junit.framework.TestCase;

public class testDAOingredients extends TestCase 
{
	DBManager manager=new DBManager("local");
	DAOingredientsMySQL dAOingredientMySQL=new DAOingredientsMySQL(manager);
	
	public void testinsertIngredient()
	{
		VOingredient vOPingredient= new VOingredient();
		vOPingredient.setName("arina");
		dAOingredientMySQL.insertIngredient(vOPingredient);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		String nombre="";
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM ingredients ORDER BY code DESC LIMIT 1");
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				nombre=resultSet.getString(2);
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		assertEquals(vOPingredient.getName(), nombre);
	}
	
	public void testupdateIngredient()
	{
		VOingredient vOPingredient= new VOingredient();
		vOPingredient.setCode(5);
		vOPingredient.setName("Harina");
		
		dAOingredientMySQL.updateIngredient(vOPingredient);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		String nombre="";
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM ingredients where code=?");
			preparedStatement.setInt(1,vOPingredient.getCode());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				nombre=resultSet.getString(2);
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		assertEquals(vOPingredient.getName(), nombre);
	}
	public void testremoveIngredient()
	{
		VOingredient vOPingredient= new VOingredient();
		vOPingredient.setCode(4);
		
		dAOingredientMySQL.removeIngredient(vOPingredient);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		VOingredient searchVoIngredient = new VOingredient();
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM ingredients where code=?");
			preparedStatement.setInt(1,vOPingredient.getCode());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				searchVoIngredient.setCode(resultSet.getInt(1));
				searchVoIngredient.setName(resultSet.getString(2));
			}
			else
			{
				searchVoIngredient=null;
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		assertEquals(searchVoIngredient,null );
	}
	public void testlistIngredients()
	{
		ArrayList<VOingredient> search=dAOingredientMySQL.listIngredients();
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		
		int count=0;
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM ingredients");
			resultSet = preparedStatement.executeQuery();
			
			while(resultSet.next())
			{
				count++;
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		assertEquals(search.size(),count );
	}
	public void testsearchIngredientByCode()
	{
		VOingredient vOPingredient= new VOingredient();
		vOPingredient.setCode(2);
		
		VOingredient search=dAOingredientMySQL.searchIngredientByCode(vOPingredient);
		
		
		assertEquals(vOPingredient.getCode(),search.getCode());
	}
	public void testsearchIngredientByName()
	{
		VOingredient vOPingredient= new VOingredient();
		vOPingredient.setName("huevos");
		
		VOingredient search=dAOingredientMySQL.searchIngredientByName(vOPingredient);		
		
		assertEquals(vOPingredient.getName(),search.getName());
	}
}
